var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : '/zofui_sales/card/sortlist',
        page : {
            isend : false,
            doing : false,
            doo : 'card',
            pdata : {
                op : 'list',
                page : 1,
                sortid : 0, // 分类id
                init : 1,
                shopsid : 0,
                ordernew : 0,
                orderfee : 0,
                orderuse : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        allsort : [],
        leftact : 0,
        color : '#ffffff',
        activeIndex:0,
        shopid : 0,
        searchs : '',
        issearch : false,
        scrolltop : 0,
        shopsort : [],
        showsort : false,
    },
    onLoad:function(options){
        var self = this;

        var zfid = options.zfid ? options.zfid : '';
        var shopid = options.shopid ? options.shopid : '';
        self.data.options = options;
        
        self.data.page.pdata.initsortlist = 1;
        self.data.page.pdata.page = 1;
        self.data.page.isloading = false;

        app.com.getLocation(self,function(loc){
            self.setData({
                'page.pdata.lng' : loc.longitude,
                'page.pdata.lat' : loc.latitude,
            });
            app.com.auth(function(user){
                app.com.getPage(self,self.data.page,20,function(res){
                    var data = res.data.data;
                    self.setData({
                       datalist : data.list,
                       params : res.data.data.params,
                       sharedata : res.data.data.sharedata,
                       allsort : data.sort,
                       shopsort : data.shopsort,
                       inited : true,
                    });
                    self.data.page.pdata.initsortlist = 0;

                    wx.setNavigationBarTitle({
                        title : '优惠券',
                    });

                },false,function(res){
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(0);
                    });
                });
                app.com.setBar(app,self,shopid);
            });
        })
        
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.data.page.isend = false;
        this.data.page.waitf = false;
        this.data.page.nodataf = false;
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
        this.data.isdown = false;
    },
    actfn(e){
        var self = this;
        self.setData({
            activeIndex : e.currentTarget.dataset.index
        })
    },
    actfnat(e){
        var self = this;
        var index = e.currentTarget.dataset.index;
        var type = e.currentTarget.dataset.type;
        if( self.data.ising ) return false;
        self.data.ising = true;

        self.data.page.isloading = true;
        self.setData({
            activeIndex : index,
            ['page.pdata.'+type] : e.currentTarget.dataset.id,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
                showsort : false,
            });
        });
        self.data.ising = false;
        
    },
    falsesearch(e){
        this.searchfn('show');
    },
    changeorder(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        if( self.data.ising ) return false;
        self.data.ising = true;
        self.data.page.isloading = true;

        var ordernew = 0;
        var orderfee = 0;
        var orderuse = 0;
        if(type == 'ordernew'){
            ordernew = self.data.page.pdata.ordernew == 3 ? 1 : self.data.page.pdata.ordernew + 1;
        }else if(type == 'orderfee'){
            orderfee = self.data.page.pdata.orderfee == 3 ? 1 : self.data.page.pdata.orderfee + 1;
        }else if(type == 'orderuse'){
            orderuse = self.data.page.pdata.orderuse == 3 ? 1 : self.data.page.pdata.orderuse + 1;
        }

        self.setData({
            'page.pdata.ordernew' : ordernew,
            'page.pdata.orderfee' : orderfee,
            'page.pdata.orderuse' : orderuse,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    onReachBottom : function(){
        var self = this;
        self.data.page.isloading = false;

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    showselect(){
        this.setData({
            showselect : !this.data.showselect
        })
    },
    togglesort(){
        this.setData({
            showsort : !this.data.showsort,
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})